package com.xiaomi.baidumap.favorite;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xiaomi.baidumap.R;
import com.xiaomi.baidumap.favorite.model.LocalRepo;
import com.xiaomi.baidumap.model.Repo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 16-9-1.
 */
public class FavoriteAdapter extends BaseAdapter {
    private List<LocalRepo> localRepos;

    public FavoriteAdapter() {
        localRepos = new ArrayList<>();
    }

    //添加数据
    public void addData(List<LocalRepo> repos){
        localRepos.clear();
        localRepos.addAll(repos);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return localRepos == null ? 0:localRepos.size();
    }

    @Override
    public Object getItem(int position) {
        return localRepos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView==null){
            convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_item_repo,parent,false);
            convertView.setTag(new ViewHolder(convertView));
        }
        ViewHolder viewHolder = (ViewHolder) convertView.getTag();
        LocalRepo localRepo = localRepos.get(position);
        viewHolder.tvRepoName.setText(localRepo.getFullName());
        viewHolder.tvRepoInfo.setText(localRepo.getDescription());
        viewHolder.tvRepoStars.setText("starts:"+localRepo.getStartsCount()+"");
        ImageLoader.getInstance().displayImage(localRepo.getAvatar(), viewHolder.ivIcon);
        return convertView;
    }
    static class ViewHolder{
        @BindView(R.id.ivIcon)
        ImageView ivIcon;
        @BindView(R.id.tvRepoName)
        TextView tvRepoName;
        @BindView(R.id.tvRepoInfo) TextView tvRepoInfo;
        @BindView(R.id.tvRepoStars) TextView tvRepoStars;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }

    }
}
